<?php
namespace app\admin\controller;

use think\Db;
use think\Exception;
use think\Loader;

class Shop extends Common
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->obj = model('shop');
    }

    public function index()
    {
        $param = input('param.');
        $this->assign('param', $param);
        $where = [];
        if (input('?param.status') && input('param.status') > 0) {
            $where['status'] = input('param.status');
        }

        if (input('?param.mobile') && input('param.mobile') > 0) {
            $member = model('member')->get(['username' => input('param.mobile')]);
            $where['member_id'] = $member->id;
        }

        $data = $this->obj->where($where)->paginate('25', false, ['query' => $param]);
        //dump($data);exit;
        //echo $this->obj->getLastSql(); exit;
        $this->assign('data', $data);
        return view();
    }

    /**
     * 店铺审核
     */
    public function audit() {
        $value = input('param.value');
        $id = input('param.id');
        $audit_msg = input('param.audit_msg', '');
        $this->obj->save(['status' => $value, 'audit_msg' => $audit_msg, 'audit_time' => time()], ['id' => $id]);
        $this->success('审核成功！');
    }

    /**
     * 店铺补货申请
     * @return \think\response\View
     */
    public function shop_replenishment_apply_for() {
        $data = model('shop_replenishment_apply_for')->order('status asc, id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }

    /**
     * 创建产品二维码
     * @return \think\response\View
     */
    public function create_rq_code() {
        Db::startTrans();
        try {
            $id = input('param.id');
            $apply_for = model('shop_replenishment_apply_for')->get(['id' => $id]);
            if ($apply_for->status != 1) {
                throw new Exception('状态异常');
            }
            //$num = $apply_for->num;
            $num = 10;
            if ($num <= 0) {
                throw new Exception('数量异常');
            }

            $data = [];
            for($i = 0; $i < $num; $i++) {
                //获取防伪码code
                $code = md5('jx_shop_id_' . $apply_for->shop_id . '_product_id_' . $apply_for->product_id . $i . date('YmdHis') . rand(1000, 9999));
                $erweima_text = request()->domain() . '/anti_fake/shop_id/' . $apply_for->shop_id . '/product_id/' . $apply_for->product_id . '/code/' . $code;  // 防伪二维码内容
                Loader::import('QRcode.phpqrcode');

                if (!is_dir(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'anti_fake' . DS . date('Y-m-d'))) {
                    mkdir(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'anti_fake' . DS . date('Y-m-d'));
                }

                $erweima_image =  'uploads' . DS . 'anti_fake' . DS . date('Y-m-d') . DS . $code . '.png';  //  防伪二维码地址
                $erweima_url = ROOT_PATH . 'public' . DS . $erweima_image;
                \QRcode::png($erweima_text, $erweima_url, '', 6, 2); //生成二维码
                //erweima_add_log($erweima_url); //添加logo

                $data[] = [
                    'product_id' => $apply_for->product_id,
                    'shop_id' => $apply_for->shop_id,
                    'code' => $code,
                    'erweima' => $erweima_image,
                    'apply_for_id' => $id
                ];
            }
            model('product_code')->saveAll($data);
            model('shop_replenishment_apply_for')->save(['status' => 2], ['id' => $id]);

            Db::commit();
            return json(['code' => 1, 'msg' => '生成成功']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 查看防伪码列表
     * @return \think\response\View
     */
    public function rq_code_list() {
        $data = model('product_code')->all(['apply_for_id' => input('param.id')]);
        $this->assign('data', $data);
        return view();
    }
}
